<?php
/**
 * Created by PhpStorm.
 * User: Antique
 * Date: 2021/12/11
 * Time: 下午 08:36
 */
namespace test\primary;

include 'mate_cookie.php';
include 'sql_action.php';

$mate_cookie = new mate_cookie();
$pd = $mate_cookie->mate();
$pd2 = 0;
$md5 = $_COOKIE['Token'];

if ($pd == 0) {
    $old_pwd = $_POST['old_pwd'];
    $new_pwd_1 = $_POST['new_pwd_1'];
    $new_pwd_2 = $_POST['new_pwd_2'];
    if ($old_pwd == null || $new_pwd_1 == null || $new_pwd_2 == null) {
        $pd2 = 1;
    }
    if ($new_pwd_1 != $new_pwd_2) {
        $pd2 = 2;
    }
    if ($old_pwd == $new_pwd_1) {
        $pd2 = 3;
    }
    if ($pd2 == 0) {
        $conn = new \mysqli();
        $conn->connect('127.0.0.1', 'root', '123456', 'userinfo');
        if (!empty($conn->connect_error)) {
            echo '数据库异常';
        }
        sql_action::set_conn($conn);
        $sql = ('update user_info set pwd=\'' . $new_pwd_1 . '\' where cookie=\'' . $md5 . '\' and pwd=\'' . $old_pwd . '\'');
        $flag = sql_action::Change($sql);
        if (!$flag) {
            $pd2 = 4;
        }
    }
}


?>


<!DOCTYPE html>
<html>
<script>
    switch (<?php echo $pd ?>) {
        case 1:
            alert('你还没登陆或者cookie失效');
            window.location.href = 'login.php';
            break;
        case 2:
            alert('cookie匹配失败');
            window.location.href = 'login.php';
            break;
        case 3:
            alert('cookie到期');
            window.location.href = 'login.php';
            break;
    }
    switch (<?php echo $pd2 ?>) {
        case 1:
            alert('输入框不能为空');
            window.location.href = 'change_pwd.php';
            break;
        case 2:
            alert('两次输入的新密码不一致');
            window.location.href = 'change_pwd.php';
            break;
        case 3:
            alert('新密码不能与旧密码相同');
            window.location.href = 'change_pwd.php';
            break;
        case 4:
            alert('你可能输入了错的旧密码，请重试');
            window.location.href = 'change_pwd.php';
            break;
        case 0:
            alert('修改成功');
            window.location.href = '../login.php';
            break;
    }
</script>
</html>








